# for QR codes use inline
# %matplotlib inline
# qr_setting = 'url'
#
# for lecture use notebook to manipulate figures
%matplotlib inline
#
qr_setting = None
#
%config InlineBackend.figure_format='retina'
# import libraries
import numpy as np
import matplotlib as mp
import pandas as pd
import matplotlib.pyplot as plt
import laUtilities as ut
import slideUtilities as sl
import demoUtilities as dm
import pandas as pd
from matplotlib import animation
from importlib import reload
from datetime import datetime
from IPython.display import Image
from IPython.display import display_html
from IPython.display import display
from IPython.display import Math
from IPython.display import Latex
from IPython.display import HTML
mp.rcParams['animation.html'] = 'jshtml';
Linear Equations¶
# image credit: http://en.wikipedia.org/wiki/The_Nine_Chapters_on_the_Mathematical_Art
display(Image("images/nine-chapters-mathematical-art.jpg",width=300))
Traditionally, algebra was the art of solving equations and systems of equations. The word algebra comes form the Arabic al-jabr which means restoration (of broken parts).
The term was first used in a mathematical sense by Mohammed al-Khowarizmi (c. 780-850) who worked at the House of Wisdom, an academy established by Caliph al Ma’mum in Baghdad.
Linear algebra, then, is the art of solving systems of linear equations.
Linear Algebra with Applications , Bretscher
Al-Khowarizmi gave his name to the algorithm. He wrote a book called “ilm al-jabr wa’l-muqābala’ which means “The science of restoring what is missing and equating like with like.”
Our entry point into linear algebra will be solving systems of linear equations.
The yield of one bundle of inferior rice, two bundles of medium grade rice, and three bundles of superior rice is 39 dou of grain. The yield of one bundle of inferior rice, three bundles of medium grade rice, and two bundles of superior rice is 34 dou. The yield of three bundles of inferior rice, two bundles of medium grain rice, and one bundle of superior rice is 26 dou. What is the yield of one bundle of each grade of rice?
Nine Chapters on the Mathematical Art , c 200 BCE, China
# image credit: http://en.wikipedia.org/wiki/The_Nine_Chapters_on_the_Mathematical_Art
display(Image("images/nine-chapters-mathematical-art.jpg",width=300))
HTML(u'<a href="http://commons.wikimedia.org/wiki/File:%E4%B9%9D%E7%AB%A0%E7%AE%97%E8%A1%93%E7%B4%B0%E8%8D%89%E5%9C%96%E8%AA%AA.jpg#/media/File:%E4%B9%9D%E7%AB%A0%E7%AE%97%E8%A1%93%E7%B4%B0%E8%8D%89%E5%9C%96%E8%AA%AA.jpg">九章算術細草圖說</a> by 中國書店海王邨公司 - <a rel="nofollow" class="external free" href="http://pmgs.kongfz.com/detail/1_158470/">http://pmgs.kongfz.com/detail/1_158470/</a>. Licensed under Public Domain via <a href="//commons.wikimedia.org/wiki/">Wikimedia Commons</a>.')
Let’s denote the unknown quantities as \(x_1\), \(x_2\), and \(x_3\). These are the yields of one bundle of inferior, medium grade, and superior rice, respectively. We can then write the problem as:
The problem then is to determine the values of \(x_1, x_2,\) and \(x_3\).
These are linear equations. No term has power other than 1.
For example, there are no terms involving \(x_1^2\), or \(x_1x_2\), or \(\sqrt{x_3}\).
Basic Definitions¶
A linear equation in the variables \(x_1, \dots, x_n\) is an equation that can be written in the form
where \(b\) and the coefficients \(a_1, \dots, a_n\) are real or complex numbers that are usually known in advance.
A system of linear equations (or linear system ) is a collection of one or more linear equations involving the same variables - say \(x_1, \dots, x_n\).
A solution of the system is a list of numbers \((s_1, s_2, \dots, s_n)\) that makes each equation a true statement when the values \(s_1, s_2, \dots, s_n\) are substituted for \(x_1, x_2, \dots, x_n,\) respectively.
The set of all possible solutions is called the solution set of the linear system.
Two linear systems are called equivalent if they have the same solution set.
A system of linear equations has
no solution, or
exactly one solution, or
infinitely many solutions.
A system of linear equations is said to be consistent if it has either one solution or infinitely many solutions.
A system of linear equations is said to be inconsistent if it has no solution.
The Geometry of Linear Equations¶
Any list of numbers \((s_1, s_2, \dots, s_n)\) can be thought of as a point in \(n\)-dimensional space, called a vector space.
We call that vector space \(\mathbb{R}^n\).
So if we are considering linear equations with \(n\) unknowns, the solutions are points in \(\mathbb{R}^n\).
Now, any linear equation defines a point set with dimension one less than the space. For example:
if we are in 2-space (2 unknowns), a linear equation defines a line.
if we are in 3-space (3 unknowns), a linear equation defines a plane.
in higher dimensions, we refer to all such sets as hyperplanes.
Question: why does a linear equation define a point-set of dimension one less than the space?
Some Examples in \(\mathbb{R}^2\)¶
How many solutions does the linear system have in each case?
fig = ut.two_d_figure('Figure 1.2d.1', size = (5,5))
fig.centerAxes()
fig.plotLinEqn( 1, -2, -1, color = 'r')
fig.plotLinEqn(-1, 3, 3, color = 'b')
plt.legend(loc='best', fontsize = 14);
This system of two equations has exactly one solution.
fig = ut.two_d_figure('Figure 1.2d.2', size = (5,5))
fig.centerAxes()
fig.plotLinEqn( 1, -2, -1, color = 'r')
fig.plotLinEqn(-1, 2, 3, color = 'b')
plt.legend(loc='best', fontsize = 14);
This system of two equations has no solutions.
fig = ut.two_d_figure('Figure 1.2d.3', size = (5,5))
fig.centerAxes()
fig.plotLinEqn( 1, -2, -1, color = 'r')
fig.plotLinEqn(-1, 2, 1, format = '--', color = 'b')
plt.legend(loc='best', fontsize = 14);
This system of equations has infinitely many solutions.
Some Examples in \(\mathbb{R}^3\)¶
How many solutions are there in each of these cases?
fig = ut.three_d_figure((1, 1), fig_desc = 'Example 1: One Solution',
xmin = -5, xmax = 5, ymin = -5, ymax = 5, zmin = -5, zmax = 5,
figsize = (6,6), qr = qr_setting, displayAxes = False)
plt.close()
eq1 = [1, 0, 0, 1]
eq2 = [0, 1, 0, -2]
eq3 = [0, 0, 1, 0]
fig.plotLinEqn(eq1, 'Brown')
fig.plotLinEqn(eq2, 'Green')
fig.plotLinEqn(eq3, 'Blue')
fig.plotIntersection(eq1, eq2, color='Blue', line_type='--')
fig.plotIntersection(eq2, eq3, color='Blue', line_type='--')
fig.plotIntersection(eq1, eq3, color='Blue', line_type='--')
# fig.set_title('Example 1')
fig.ax.view_init(azim = 0, elev = 22)
fig.save()
#
def anim(frame):
fig.ax.view_init(azim = frame, elev = 22)
# fig.canvas.draw()
#
# create the animation
animation.FuncAnimation(fig.fig, anim,
frames = 2 * np.arange(180),
fargs = None,
interval = 25,
repeat = False)
fig = ut.three_d_figure((1, 2), fig_desc = 'Example 2: No Solutions',
xmin = -5, xmax = 5, ymin = -5, ymax = 5, zmin = -5, zmax = 5,
figsize = (6,6), qr = qr_setting, displayAxes = False)
plt.close()
# equation of a line from its normal a is a'x = a'a
# three normals 120 degrees spread around the y axis
eq1 = [np.sqrt(3/4), 0, 1/2, 1]
eq2 = [-np.sqrt(3/4), 0, 1/2, 1]
eq3 = [0, 0, -1, 2]
fig.plotLinEqn(eq1, 'Brown')
fig.plotLinEqn(eq2, 'Green')
fig.plotLinEqn(eq3, 'Blue')
fig.plotIntersection(eq1, eq2, color='Blue', line_type='--')
fig.plotIntersection(eq2, eq3, color='Blue', line_type='--')
fig.plotIntersection(eq1, eq3, color='Blue', line_type='--')
# fig.set_title('Example 2')
fig.ax.view_init(azim = 0, elev = 22)
fig.save()
#
def anim(frame):
fig.ax.view_init(azim = frame, elev = 22)
# fig.canvas.draw()
#
# create the animation
animation.FuncAnimation(fig.fig, anim,
frames = 2 * np.arange(180),
fargs = None,
interval = 25,
repeat = False)